﻿/**
 * Created in 2012-2-21.
 */
package kit4j.tests.jdk.reflect;

import java.io.IOException;
import java.io.InputStream;

import kit4j.common.io.TxtExtractor;

/**
 * 
 * @author MiniKnife
 * 
 */
public class TestLoadResource {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		loadResourceByClassLoader("com/surekam/jdk/reflect/test.txt");
		System.out.println();
		
		loadResourceByAbsolutePath("/com/surekam/jdk/reflect/test.txt");
		System.out.println();
		
		loadResourceByRelativePath("test.txt");
	}
	
	static void loadResourceByClassLoader(String resourceName) {
		printLoadResourceInfo(resourceName, ClassLoader.getSystemResourceAsStream(resourceName));
	}
	
	static void loadResourceByAbsolutePath(String resourceName) {
		printLoadResourceInfo(resourceName, TestLoadResource.class.getResourceAsStream(resourceName));
	}
	
	static void loadResourceByRelativePath(String resourceName) {
		printLoadResourceInfo(resourceName, TestLoadResource.class.getResourceAsStream(resourceName));
	}

	static void printLoadResourceInfo(String resourceName, InputStream resourceStream) {
		System.out.println("load resource: " + resourceName);

		if (resourceStream != null) {
			System.out.println("resource content:");

			try {
				printInputStream(resourceStream);
			} catch (IOException e) {
				e.printStackTrace();
			}
		} else {
			System.out.println("load failed.");
		}
	}

	static void printInputStream(InputStream is) throws IOException {
		System.out.println(TxtExtractor.getText(is));
	}

}
